package com.sundablog.utlis;


import java.util.HashMap;
import java.util.Map;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.util.FormatUtil;

/**
 * Java系统监控测试类
 * 
 * @ClassName: SystemInfoTest
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: 哒哒
 * @date: 2018年3月1日 下午5:33:51
 * 
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
public class SystemInfoTest {
	
	/**
	 * 获取系统内存
	 * @Title: Memory   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param memory
	 * @param: @return      
	 * @return: Map<String,String>      
	 * @throws
	 */
	public static Map<String, String> Memory(GlobalMemory memory) {
		long available = memory.getAvailable();
		long total = memory.getTotal();
		double aa = (available / (double) total) * 100;
		int percentage = (int) aa;
		Map<String, String> map = new HashMap<>();
		map.put("available", FormatUtil.formatBytes(memory.getAvailable()));
		map.put("total", FormatUtil.formatBytes(memory.getTotal()));
		map.put("Percentage", String.valueOf(percentage));
		return map;
	}
	/**
	 * 获取系统cpu百分百
	 * @Title: Cpu   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param processor
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int Cpu(CentralProcessor processor) {
		int cpu = (int)processor.getSystemCpuLoadBetweenTicks() * 100;
		return cpu;
	}

	public static void main(String[] args) {
		SystemInfo si = new SystemInfo();

		HardwareAbstractionLayer hal = si.getHardware();
		for (int i = 0; i < 2000; i++) {
			Cpu(hal.getProcessor());
		}
	}

}
